<template>
  <div class="flex flex-col h-full">
    <SearchContainer @reset="() => formRef.resetFields()" @search="getData()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="bh" class="w-72 mb-0">
          <a-input v-model:value="formState.bh" placeholder="员工编号/员工姓名/助记码" allow-clear @blur="getData()" />
        </a-form-item>
        <a-form-item name="status" class="w-50 mb-0 ml-8">
          <a-radio-group v-model:value="formState.status" @change="getData()">
            <a-radio :value="0">全部</a-radio>
            <a-radio :value="1">在职</a-radio>
            <a-radio :value="2">医护</a-radio>
          </a-radio-group>
        </a-form-item>
        <!-- <a-form-item name="status" class="w-72 mb-0 ml-8">
          <a-radio-group v-model:value="formState.status" @change="getData()">
            <a-radio :value="0">全部</a-radio>
            <a-radio :value="1">未对照</a-radio>
            <a-radio :value="2">已对照</a-radio>
          </a-radio-group>
        </a-form-item> -->
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3 flex justify-end">
        <BaseExport type="医保" name="医保人员对照" code="000779" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable column-code="000779" fill :loading="loading" :data-source="tableData" bordered :pagination="pagination" @change="onPageChange">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'shInsureNo'">
            <EditCell title="医保工号">
              <a-input v-model:value="record.shInsureNo" placeholder="请输入" @change="record.isChange = true" @blur="onBlurInput(record, $event)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'certNo'">
            <EditCell title="执业证书编号">
              <a-input v-model:value="record.certNo" placeholder="请输入" @change="record.isChange = true" @blur="onBlurInput(record, $event)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'nmiCode'">
            <EditCell title="国家医保编码">
              <a-input v-model:value="record.nmiCode" placeholder="请输入" @change="record.isChange = true" @blur="onBlurInput(record, $event)" />
            </EditCell>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'

const formRef = ref(null)
const formState = ref({ status: 0 })
const { loading, tableData, getData, pagination } = useTable({
  url: '/insure/getEmpInsure',
  immediate: true,
  params: formState.value
})
const onBlurInput = async (record) => {
  if ((!record.shInsureNo && !record.certNo && !record.nmiCode) || !record.isChange) return
  record.isChange = false
  const { code, msg } = await post('/insure/saveEmpInsure', {
    ...record
  })
  if (code !== 0) return message.error(msg || '保存失败')
  message.success(msg || '保存成功')
  getData()
}
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
